//第一层
var express = require('express');
var router = express.Router();
const jwt  = require('jsonwebtoken')
const { pAdmin, pAdminId, pAdminUpdate, shopList, shopId, pAdminUpdateShop, loginer } = require('../service/pAdminService')

//获取所有商家数据
router.get('/pAdmin', async function (req, res, next) {
    res.send(await pAdmin(req.query))
})

//根据_id获取商家数据
router.get('/pAdminId', async function (req, res, next) {
    const _id = req.query
    res.send(await pAdminId(_id))
})

//得到已冻结的商家数据
router.post('/pAdminUpdate', async function (req, res, next) {
    const all = req.body
    res.send(await pAdminUpdate(all))
})

//获取所有店铺数据
router.get('/shopList', async function (req, res, next) {
    res.send(await shopList(req.query))
})

//根据_id获取店铺数据
router.get('/shopId', async function (req, res, next) {
    const _id = req.query
    res.send(await shopId(_id))
})

//得到已冻结的店铺数据
router.post('/pAdminUpdateShop', async function (req, res, next) {
    const all = req.body
    res.send(await pAdminUpdateShop(all))
})

//平台管理员登录
router.get('/loginer', async function (req, res, next) {
    const data = await loginer(req.query)
    if (data) {
        const token = jwt.sign({ _id: data._id}, 'myKey', { expiresIn: 20 })
        res.send({ success: true, token })
    } else {
        res.send({ success: false })
    }

})
const { login2, Add2 } = require('../service/pAdminService')
//商家登陆
router.get('/login', async function (req, res, next) {
    const data = await login2(req.query);
    res.send(data)
})

//商家注册
router.post('/register', async function (req, res, next) {
    const data = await Add2(req.body);
    res.send(data)
})

module.exports = router;